//
//  AccountViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/20.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit
import SwiftyJSON

class AccountViewController: KMViewController {
    
    fileprivate let accountView = AccountView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        NotificationCenter.default.addObserver(self, selector: #selector(updateBill(sender:)), name: .addBillPush, object: nil)
        
        addSubView()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: .addBillPush, object: nil)
    }
    
    //MARK: 更新账单
    @objc fileprivate func updateBill(sender:Notification) {
        
        let json = JSON.init(sender.userInfo!)
        
        if let dic = json.dictionaryObject {
            
            if let type = dic["type"] as? String {
                if type == "bank" {
                    let model = InputBillModel.setModel(with: dic)
                    self.accountView.dataArray.append(model)
                    
                    NSKeyedArchiver.archiveRootObject(self.accountView.dataArray, toFile: cachesDir) //账单数据归档
                    
                }else if type == "life" {
                    let model = LifeBillModel.setModel(with: dic)
                    self.accountView.dataArray.append(model)
                    
                    NSKeyedArchiver.archiveRootObject(self.accountView.dataArray, toFile: cachesDir) //账单数据归档
                }
            
                
            }
            
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
    self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
        self.navigationController?.navigationBar.shadowImage = nil
    }
    
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
    
        self.navigationController?.navigationBar.isTranslucent = true
        accountView.accountDelegate = self
        self.view.addSubview(accountView)
        
        accountView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: -StatusBarAddNavBarHeight, left: 0, bottom: 0, right: 0))
        }
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    

}

extension AccountViewController : AccountViewDelegate {
    
    //MARK: 显示标题
    func showTitleView(y: CGFloat) {
        
        let height = y + StatusBarAddNavBarHeight
        
        if height < 0 {
            self.accountView.headView.redView.snp.remakeConstraints{ (make) in
                make.top.equalToSuperview().offset(height)
                make.left.right.equalToSuperview()
                make.height.equalTo(RATIO_H(maxNum: 200) - height)
            }
        }
        
        self.accountView.headView.titleLabel.text = "HI, " + self.accountView.headView.getCurrentTime()
        
//        if self.accountView.tableView.contentSize.height > self.accountView.tableView.frame.size.height && y > self.accountView.tableView.contentSize.height - self.accountView.tableView.frame.size.height {
//
//            self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
//            self.title = account_title
//        }
        
        UIMenuController.shared.setMenuVisible(false, animated: true) //隐藏操作框
        
    }
    
    //MARK: 添加账单
    func addBillAction() {
        
        let addBillVC = AddBillViewController()
        addBillVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(addBillVC, animated: true)
    }
    
//    //MARK: 账单设置
//    func setBillAction(indexPath:IndexPath) {
//
//
//
//        let menuController = UIMenuController.shared
//        if menuController.isMenuVisible {
//            menuController.setMenuVisible(false, animated: true)
//        }else{
//
//            let cell = self.accountView.tableView.cellForRow(at: indexPath)
//                //成为第一响应者，需重写该方法
//            cell?.becomeFirstResponder()
//
//                //添加menuItem
//                let deleteItem = UIMenuItem.init(title: delete_title, action: #selector(deleteBill))
//                let topItem = UIMenuItem.init(title: goTop, action: #selector(makeTopBill))
//                menuController.menuItems = [deleteItem,topItem]
//            let showRect = CGRect.init(x: (cell?.frame.origin.x)!, y: (cell?.frame.origin.y)! + ((cell?.frame.size.height)!/2), width: (cell?.frame.size.width)!, height: (cell?.frame.size.height)!)
//            menuController.setTargetRect(showRect, in: (cell?.superview!)!)
//                menuController.setMenuVisible(true, animated: true)
//
//        }
//    }
//
//    //MARK: 删除账单
//    @objc fileprivate func deleteBill() {
//
//        self.accountView.dataArray.remove(at: self.accountView.currentIndex)
//        self.accountView.tableView.reloadData()
//        NSKeyedArchiver.archiveRootObject(self.accountView.dataArray, toFile: cachesDir) //账单数据归档
//    }
//
//    //MARK: 账单置顶
//    @objc fileprivate func makeTopBill() {
//
//        let model = self.accountView.dataArray[self.accountView.currentIndex]
//        self.accountView.dataArray.remove(at: self.accountView.currentIndex)
//        self.accountView.dataArray.insert(model, at: 0)
//        self.accountView.tableView.reloadData()
//        NSKeyedArchiver.archiveRootObject(self.accountView.dataArray, toFile: cachesDir) //账单数据归档
//    }
    
    //MARK: 去还款
    func toReypayment(index: Int) {
        
        let payVC = PayViewController()
        payVC.modalPresentationStyle = .overCurrentContext
        payVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        payVC.showTabBar = {
           self.tabBarController?.tabBar.isHidden = false
        }
        
        payVC.payBlock = {
            if self.accountView.dataArray[index] is InputBillModel {
                (self.accountView.dataArray[index] as? InputBillModel)?.isPay = "1"
                self.accountView.tableView.reloadData()
            NSKeyedArchiver.archiveRootObject(self.accountView.dataArray, toFile: cachesDir) //账单数据归档
            }
            
            if self.accountView.dataArray[index] is LifeBillModel {
                (self.accountView.dataArray[index] as? LifeBillModel)?.isPay = "1"
                self.accountView.tableView.reloadData()
            NSKeyedArchiver.archiveRootObject(self.accountView.dataArray, toFile: cachesDir) //账单数据归档
            }
        }
        
        self.present(payVC, animated: false, completion: {
            self.tabBarController?.tabBar.isHidden = true
        })
    }
    
    //MARK: 查看账单详情
    func showBillDetails(model: BillModel) {
        
        let billDetailsVC = CreditCardBillViewController.init(model: model)
        billDetailsVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(billDetailsVC, animated: true)
    }
}
